package com.ps.actions.setting;

import java.sql.Timestamp;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.springframework.beans.factory.annotation.Autowired;

import com.ps.actions.BaseAction;
import com.ps.entity.Result;
import com.ps.pojo.setting.Area;
import com.ps.pojo.setting.Role;

import com.ps.service.setting.AreaService;
import com.ps.util.Kit;
import com.ps.util.PaginationSupport;

public class AreaAction extends BaseAction {
	@Autowired 
	private AreaService areaService;
	
	private String paginationJson;
	
	private String areaJson;
	
	private Integer areaId;
	

	@SuppressWarnings("unchecked")
	public void findByPage() {
		System.out.println("paginationJson = " + paginationJson);
		PaginationSupport paginationSupport = Kit.getFromJson(paginationJson, PaginationSupport.class);
		DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Area.class);
		Result result = areaService.findPageByCriteria(detachedCriteria, paginationSupport.getPageSize(), paginationSupport.getStartIndex(), Order.desc("areaId"));
		System.out.println(Kit.toJson(result));
		this.outputJson(Kit.toJson(result));
	}

	public void addArea() {
		Area area = Kit.getFromJson(areaJson, Area.class);
		area.setInTime(new Timestamp(System.currentTimeMillis()));
		Result result = areaService.save(area);
		this.outputJson(Kit.toJson(result));
	}
	
	public void updateArea() {

		Area area = Kit.getFromJson(areaJson, Area.class);
		area.setInTime(new Timestamp(System.currentTimeMillis()));
		Result result = areaService.update(area);
		result.setDatas(area.getAreaId());
		//System.out.println(Kit.toJson(result));
		this.outputJson(Kit.toJson(result));
	}
	
	public void deleteArea() {
		Result result = areaService.delete(areaId);
		this.outputJson(Kit.toJson(result));
	}
	
	/**
	 * 查找全部区域
	 */
	public void findAllArea() {
		outputJson(Kit.toJson(areaService.findAll()));
	}	
	
	
	public String getPaginationJson() {
		return paginationJson;
	}

	public void setPaginationJson(String paginationJson) {
		this.paginationJson = paginationJson;
	}
	
	public String getAreaJson() {
		return areaJson;
	}

	public void setAreaJson(String areaJson) {
		this.areaJson = areaJson;
	}

	public Integer getAreaId() {
		return areaId;
	}

	public void setAreaId(Integer areaId) {
		this.areaId = areaId;
	}

	
}
